<?php

namespace services\member;

use api\modules\v1\forms\member\MemberForm;
use common\components\Service;
use common\models\member\Attr;
use common\models\member\Member;
use services\rbac\UserRoleService;
use Yii;
use yii\db\Expression;

/**
 * Class AttrService
 * @package services\member
 *
 */
class AttrService extends Service
{
    /**
     * 根据项目ID获取人员attr
     * @param int $projectId
     * @return Attr[]
     */
    public static function getAttrListByProjectId(int $projectId): array
    {
        return Attr::find()
            ->where(new Expression("JSON_CONTAINS(project_id, '{$projectId}')"))
            ->all();
    }

    /**
     * 根据部门ID获取人员attr
     * @param int|array $departmentIds
     * @return Attr[]
     */
    public static function getAttrListByDepartmentId($departmentIds): array
    {
        $memberIds = MemberDepartmentService::getMemberIdsByDepartmentId($departmentIds);

        return Attr::find()
            ->where(['member_id' => $memberIds])
            ->all();
    }

    /**
     * 编辑项目
     * @param Member $member
     * @param MemberForm $model
     * @return Attr
     * @throws \Exception
     */
    public static function editModel(Member $member, MemberForm $model): Attr
    {
        $projectList = $model->getProjectList();
        $departmentList = $model->getDepartmentList();
        $job = $model->getJob();
        $attr = $model->getMemberAttr();
        if (empty($attr)) {
            $attr = new Attr();
        }

        $attr->member_id = $member->id;
        $attr->project_id = $projectList ? array_column($projectList, 'id') : [];
        $attr->department_id = $departmentList ? array_column($departmentList, 'id') : [];
        $attr->job_id = $job ? $job->id : 0;
        $attr->operation_id = Yii::$app->user->identity->member_id;
        $attr->saveAndCheckResult();

        // 同步绑定
        MemberProjectService::editModel($member, $attr->project_id);
        MemberDepartmentService::editModel($member, $attr->department_id);

        return $attr;
    }

    /**
     * 解绑项目
     * @param int $projectId
     * @return void
     */
    public static function unBindProject(int $projectId): void
    {
        $attrs = self::getAttrListByProjectId($projectId);
//        $members = MemberService::getNormalListByIds(array_column($attrs, 'member_id'));
//        $memberIds = array_column($members, 'id');
//        if ($memberIds) {
            foreach ($attrs as $attr) {
                /*if (in_array($attr->member_id, $memberIds) === false) {
                    continue;
                }*/
                /**
                 * @var int[] $projectIds
                 */
                $projectIds = $attr->project_id;
                unset($projectIds[array_search($projectId, $projectIds)]);
                $attr->project_id = array_values($projectIds);
                $attr->saveAndCheckResult();
            }
//        }
        // 同步解绑
        MemberProjectService::unBindProject($projectId);
    }

    /**
     * 解绑部门
     * @param int|array $ids
     * @param array $selectRoleIds
     * @return void
     */
    public static function unBindDepartment($ids, array $selectRoleIds): void
    {
        // 部门下人员角色删除
        UserRoleService::deleteDepartmentRole($ids, $selectRoleIds);

        $attrs = self::getAttrListByDepartmentId($ids);
        foreach ($attrs as $attr) {
            $attr->department_id = array_values(array_diff($attr->department_id, $ids));
            $attr->saveAndCheckResult();
        }
        // 同步解绑
        MemberDepartmentService::unBindDepartment($ids);
    }

    /**
     * 解绑岗位
     * @param int $jobId
     * @return void
     */
    public static function unBindJob(int $jobId): void
    {
        Attr::updateAll(['job_id' => 0], ['job_id' => $jobId]);
    }
}
